note
	component:   "openEHR ADL Tools"
	description: "parent of all ADL serialisers"
	keywords:    "ADL, serialiser"
	author:      "Thomas Beale <thomas.beale@openehr.org>"
	support:     "http://www.openehr.org/issues/browse/AWB"
	copyright:   "Copyright (c) 2003- The openEHR Foundation <http://www.openEHR.org>"
	license:     "Apache 2.0 License <http://www.apache.org/licenses/LICENSE-2.0.html>"

class ARCHETYPE_ADL_SERIALISER

inherit
	ARCHETYPE_MULTIPART_SERIALISER
		redefine
			make
		end

	ADL_2_TOKENS
		export
			{NONE} all
		end

	ADL_SYNTAX_CONVERTER
		export
			{NONE} all
		end

create
	make

feature -- Initialisation

	make (a_profile: SERIALISATION_PROFILE)
			-- set profile
		do
			precursor (a_profile)
			create last_string_value.make_empty
		end

feature -- Serialisation

	serialise (an_archetype: ARCHETYPE)
		do
		end

	serialise_from_parts (an_archetype: ARCHETYPE;
				lang_serialised, desc_serialised, def_serialised,
				rules_serialised, term_serialised,
				ann_serialised, comp_terms_serialised: STRING)
		do
			last_result.wipe_out

			archetype := an_archetype

			serialise_initialise
			serialise_archetype_id

			-- specialise statement, unless it is an OPT
			serialise_archetype_specialise

			-- languages section
			if not lang_serialised.is_empty then
				last_result.append (apply_style(symbol(SYM_LANGUAGE), STYLE_KEYWORD) + format_item(FMT_NEWLINE))
				last_result.append (lang_serialised)
			end

			-- description section
			if not desc_serialised.is_empty then
				last_result.append (format_item(FMT_NEWLINE) + apply_style(symbol(SYM_DESCRIPTION), STYLE_KEYWORD) + format_item(FMT_NEWLINE))
				last_result.append (desc_serialised)
			end

			-- definition section
			if not def_serialised.is_empty then
				last_result.append (format_item(FMT_NEWLINE) + apply_style(symbol(SYM_DEFINITION), STYLE_KEYWORD) + format_item(FMT_NEWLINE))
				last_result.append (def_serialised)
			end

			-- rules section
			if not rules_serialised.is_empty then
				last_result.append (format_item(FMT_NEWLINE) + apply_style(symbol(SYM_RULES), STYLE_KEYWORD) + format_item(FMT_NEWLINE))
				last_result.append (rules_serialised)
			end

			-- terminology section
			if not term_serialised.is_empty then
				last_result.append (format_item(FMT_NEWLINE) + apply_style(symbol(SYM_TERMINOLOGY), STYLE_KEYWORD) + format_item(FMT_NEWLINE))
				last_result.append (term_serialised)
			end

			-- annotations section
			if not ann_serialised.is_empty then
				last_result.append (format_item(FMT_NEWLINE) + apply_style(symbol(SYM_ANNOTATIONS), STYLE_KEYWORD) + format_item(FMT_NEWLINE))
				last_result.append (ann_serialised)
			end

			-- (templates) component terminologies
			if not comp_terms_serialised.is_empty then
				last_result.append (format_item(FMT_NEWLINE) + apply_style(symbol(SYM_COMPONENT_TERMINOLOGIES), STYLE_KEYWORD) + format_item(FMT_NEWLINE))
				last_result.append (comp_terms_serialised)
			end

			serialise_finalise
		end

	serialise_initialise
		local
			s: STRING
		do
			last_result.append (format_item(FMT_DOC_START))
			last_result.append (format_item(FMT_DOC_STYLES))

			-- title
			s := format_item (FMT_START_TITLE).twin
			s.replace_substring_all ("$title", archetype.artefact_type + " " + archetype.archetype_id.physical_id)
			s.append (format_item(FMT_END_TITLE))

			-- meta-data
			s := format_item (FMT_META_ITEM).twin
			s.replace_substring_all ("$name", "description")
			s.replace_substring_all ("$content", "Generated by openEHR Archetype Workbench - see " + ADL_help_page_url)
			last_result.append (s)

			last_result.append (format_item (FMT_START_BODY))
		end

	serialise_archetype_id
			-- serialise artefact type, top-level meta-data and archetype id
		local
			arch_kw_str, kw_list: STRING
		do
			arch_kw_str := archetype.artefact_type

			if attached {AUTHORED_ARCHETYPE} archetype as auth_arch then
				arch_kw_str.append (" (")
				create kw_list.make_empty

				-- adl_version
				if auth_arch.is_differential then
					kw_list.append (symbol (SYM_ADL_VERSION) + "=" + auth_arch.adl_version)
				else
					kw_list.append (symbol (SYM_ADL_VERSION) + "=" + latest_adl_version)
				end

				-- rm_release
				if not kw_list.is_empty then
					kw_list.append ("; ")
				end
				kw_list.append (symbol (SYM_RM_RELEASE) + "=" + auth_arch.rm_release)

				-- is_controlled flag
				if auth_arch.is_controlled then
					if not kw_list.is_empty then
						kw_list.append ("; ")
					end
					kw_list.append (symbol (SYM_IS_CONTROLLED))
				end

				-- is_generated flag
				if auth_arch.is_generated then
					if not kw_list.is_empty then
						kw_list.append ("; ")
					end
					kw_list.append (symbol (SYM_IS_GENERATED))
				end

				-- uid
				if attached auth_arch.uid as a_uid then
					if not kw_list.is_empty then
						kw_list.append ("; ")
					end
					kw_list.append (symbol (SYM_UID)  + "=" + a_uid.out)
				end

				-- build_uid
				if attached auth_arch.build_uid as a_uid then
					if not kw_list.is_empty then
						kw_list.append ("; ")
					end
					kw_list.append (symbol (SYM_BUILD_UID)  + "=" + a_uid.out)
				end

				-- other metadata
				if attached auth_arch.other_metadata as omd then
					across omd as omd_csr loop
						if not kw_list.is_empty then
							kw_list.append ("; ")
							kw_list.append (omd_csr.key + "=" + omd_csr.item)
						end
					end
				end

				arch_kw_str.append (kw_list)
				arch_kw_str.append_character(')')
			end

			last_result.append (apply_style (arch_kw_str, STYLE_KEYWORD) + format_item(FMT_NEWLINE))

			last_result.append (create_indent(1) + apply_style (archetype.archetype_id.as_string, STYLE_IDENTIFIER) +
				format_item(FMT_NEWLINE))
			last_result.append (format_item(FMT_NEWLINE))
		end

	serialise_archetype_specialise
		do
			if attached archetype.parent_archetype_id as pid then
				last_result.append (apply_style(symbol(SYM_SPECIALIZE), STYLE_KEYWORD) + format_item(FMT_NEWLINE))
				last_result.append (create_indent(1) + pid + format_item(FMT_NEWLINE))
				last_result.append (format_item(FMT_NEWLINE))
			end
		end

	serialise_finalise
		do
			last_result.append (format_item(FMT_END_BODY))
			last_result.append (format_item(FMT_DOC_END))
		end

end


